<?php
/**
 * 创建验证文件
 */
namespace plugins\qbt_custom_model\service;
use think\Db;

class CreateValidateService{
    /**
     * 生成验证
     */
    public static function build_validate($model_info){
        $path = str_replace("\\",DS,dirname(__DIR__)).DS.'data'.DS.'tpl'.DS.'validate.tpl';
        $model_name_arr = explode("_",$model_info['model_tabname']);
        //首字母转大写
        $mname='';
        if(count($model_name_arr)>1){
            foreach ($model_name_arr as $v){
                $mname.= ucfirst($v);
            }
//            $mname = ucfirst($model_name_arr[0]).ucfirst($model_name_arr[1]);
        }else{
            $mname.= ucfirst($model_name_arr[0]);
        }

        //验证名
        $validate_name = $mname."Validate";

        $fileName = APP_PATH."admin/validate/".$mname."Validate.php";

        $field_info = Db::name("model_field")->where(["model_id"=>$model_info['model_id']])->select()->toArray();
        $content  = file_get_contents($path);

        $arr = self::replace_all($field_info);
        //开始替换
        ob_start();
        echo str_replace(array("#rule#","#message#","#VALIDATE_NAME#"),array($arr[0],$arr[1],$validate_name),$content);
        $info2 = ob_get_contents();
        file_put_contents($fileName,$info2);
        ob_end_clean();
    }

    /**
     * 开始替换
     */
    public static function replace_all($field_info){
        $rule2 = [];
        $message = '';

        foreach ($field_info as $k=>$v){
            if($v['field_is_required'] == 1){

                if($v['field_type']=='images'||$v['field_type']=='files'){
                    $rule2[$v['field_name'].'_urls'][] = "require";
                    $message .= '
                "'.$v['field_name'].'_urls'.'.require" => "'.$v['field_nikename'].'必须填写",';
                }else{
                    $rule2[$v['field_name']][] = "require";
                    $message .= '
                "'.$v['field_name'].'.require" => "'.$v['field_nikename'].'必须填写",';
                }

            }

            if("" != $v['field_regex']){
                $rule2[$v['field_name']][] = 'regex:'.$v['field_regex'];
                $message .= '
                "'.$v['field_name'].'.regex" => "'.$v['field_nikename'].'填写有误！",';
            }

            if($v['field_min_length']>0 && $v['field_max_length']>0 && $v['field_max_length']>$v['field_min_length']){
                $rule2[$v['field_name']][] = 'min:'.$v['field_min_length'].'|max:'.$v['field_max_length'];
                $message .= '
                "'.$v['field_name'].'.min" => "'.$v['field_nikename'].'不得小于！",'.$v['field_min_length'].',';
                $message .= '
                "'.$v['field_name'].'.max" => "'.$v['field_nikename'].'不得大于！",'.$v['field_max_length'].',';
            }


        }
        $rule = '';
       $rule3 = [];
        foreach ($rule2 as $k=>$v){
           if(!empty($v)){
               $rule3[$k] = implode("|",$v);
           }
        }

        foreach ($rule3 as $k=>$v){
            $rule .='
        "'.$k.'" => "'.$v.'",';
        }
        //$rule = str_replace('|",','",',$rule);
        return [$rule,$message];
    }
}